package com.cupdata.marathon.generator;

import com.cupdata.marathon.param.download.DownloadRequest;
import com.cupdata.marathon.utils.IdUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;


@Component
public class CouponIdGenerator {
    private static String IP_16 = "ffffffff";
    private static AtomicInteger count = new AtomicInteger(1000);
    private static AtomicInteger count2 = new AtomicInteger(100);

    @Value(value = "${snowflake.cluster-id}")
    public  void setCluster_id(String cluster_id) {
        CouponIdGenerator.cluster_id = cluster_id;
    }
    @Value(value = "${snowflake.worker-id}")
    public  void setWorker(String worker_id) {
        CouponIdGenerator.worker_id = worker_id;
    }


    private static String cluster_id;


    private static String worker_id;


    static {
        try {
            String ipAddress = IdUtils.getInetAddress();
            if (ipAddress != null) {
                IP_16 = getIP_16(ipAddress);
                System.out.println("#########本机IP地址获取为"+IP_16+"################");
            }
        } catch (Throwable e) {
            /*
             * empty catch block
             */
        }
    }

    private static String getTraceId(String prefix,String worktag, long timestamp, int nextIdpre, int nextId) {
        StringBuilder appender = new StringBuilder(32);
        appender.append(prefix).append(worktag).append(timestamp).append(nextIdpre).append(nextId);
        return appender.toString();
    }

    public static String generate(DownloadRequest request) {

        return getTraceId(request.getCityCd()+request.getCouponTp(),cluster_id+worker_id, System.currentTimeMillis(),getNextIdPre(), getNextId());
    }

    private static String getIP_16(String ip) {
        String[] ips = ip.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (String column : ips) {
            String hex = Integer.toHexString(Integer.parseInt(column));
            if (hex.length() == 1) {
                sb.append('0').append(hex);
            } else {
                sb.append(hex);
            }

        }
        return sb.toString();
    }


    private static int getNextId() {
        for (;;) {
            int current = count.get();
            int next = (current > 9000) ? 1000 : current + 1;
            if (count.compareAndSet(current, next)) {
                return next;
            }
        }
    }
    private static int getNextIdPre() {
        for (;;) {
            int current = count2.get();
            int next = (current > 900) ? 200 : current + 1;
            if (count2.compareAndSet(current, next)) {
                return next;
            }
        }
    }

    public static void main(String[] args) {
        DownloadRequest request =  new DownloadRequest();
        request.setCityCd("111111");
        request.setCouponTp("2222");
        System.out.println(generate( request));

    }
}
